package cn.gson.crm.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.gson.crm.model.Jdbc;
import cn.gson.crm.model.entity.Clazz;

public class ClazzDao {
	
	/**
	 * 新增班级
	 * @throws SQLException 
	 */
	public boolean insertClazz(Clazz c) throws SQLException{
		String sql="insert into clazz(clazz_name,clazz_teacher_id) value(?,?)";
		return Jdbc.getInstace().executeUpdate(sql, c.getClazzName(),c.getClazzTeacherId());
	}
		/**
		 * 查询所有班级
		 * @throws SQLException 
		 */
		public List<Clazz> getClazz() throws SQLException{
			String sql="select * from clazz";
			List<Map<String,Object>> map=Jdbc.getInstace().executeQueryForList(sql);
			List<Clazz> l=null;
			if(map!=null&&map.size()>0){
				l=new ArrayList<Clazz>();
				for (Map<String,Object> m : map) {
					l.add(buid(m));
				}
				
			}
			return l;
		}
		/**
		 * 查询班级是否存在
		 * @throws SQLException 
		 */
		public Clazz getClazzByName(String name) throws SQLException{
			String sql="select * from clazz where clazz_name=?";
			List<Map<String,Object>> map=Jdbc.getInstace().executeQueryForList(sql,name);
			Clazz l=null;
			if(map!=null&&map.size()>0){
				l=new Clazz();
				for (Map<String,Object> m : map) {
					l=(buid(m));
				}
				
			}
			return l;
		}
		/**
		 * 根据现班级查询其他班级
		 * @throws SQLException 
		 */
		public List<Clazz> getClazz(String name) throws SQLException{
			String sql="select * from clazz  where clazz_name <> ?";
			List<Map<String,Object>> map=Jdbc.getInstace().executeQueryForList(sql,name);
			List<Clazz> l=null;
			if(map!=null&&map.size()>0){
				l=new ArrayList<Clazz>();
				for (Map<String,Object> m : map) {
					l.add(buid(m));
				}
				
			}
			return l;
		}
		/**
		 * 绑定参数
		 */
		private Clazz buid(Map<String,Object> map){
			Clazz c=new Clazz();
			if(map.get("clazz_id")!=null){
				c.setClazzId(Integer.parseInt(map.get("clazz_id").toString()));
			}
			if(map.get("clazz_name")!=null){
				c.setClazzName(map.get("clazz_name").toString());
			}
			if(map.get("clazz_teacher_id")!=null){
				c.setClazzTeacherId(Integer.parseInt(map.get("clazz_teacher_id").toString()));
			}
			return c;
		}
}
